/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import util.filter.Predicate;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_language_info_requeriment_and_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchLanguageInfoRequerimentAndOperator.findAll", query = "SELECT s FROM SearchLanguageInfoRequerimentAndOperator s"),
    @NamedQuery(name = "SearchLanguageInfoRequerimentAndOperator.findByIdSearchLanguageInfoRequerimentAndOperator", query = "SELECT s FROM SearchLanguageInfoRequerimentAndOperator s WHERE s.idSearchLanguageInfoRequerimentAndOperator = :idSearchLanguageInfoRequerimentAndOperator"),
    @NamedQuery(name = "SearchLanguageInfoRequerimentAndOperator.findByRequiredVideo", query = "SELECT s FROM SearchLanguageInfoRequerimentAndOperator s WHERE s.requiredVideo = :requiredVideo")})
public class SearchLanguageInfoRequerimentAndOperator implements Serializable, Predicate<Candidate> {
    @JoinColumn(name = "WRITING_LEVEL", referencedColumnName = "ID_LANGUAGE_LEVEL")
    @ManyToOne
    private LanguageLevel writingLevel;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_LANGUAGE_INFO_REQUERIMENT_AND_OPERATOR")
    private Integer idSearchLanguageInfoRequerimentAndOperator;
    @Column(name = "REQUIRED_VIDEO")
    private Boolean requiredVideo;
    @JoinColumn(name = "LANGUAGE", referencedColumnName = "ID_LANGUAGE")
    @ManyToOne(fetch = FetchType.LAZY)
    private Language language;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MINIMUM_LANGUAGE_LEVEL", referencedColumnName = "ID_LANGUAGE_LEVEL")
    @ManyToOne(fetch = FetchType.LAZY)
    private LanguageLevel minimumLanguageLevel;

    public SearchLanguageInfoRequerimentAndOperator() {
    }

    public SearchLanguageInfoRequerimentAndOperator(Integer idSearchLanguageInfoRequerimentAndOperator) {
        this.idSearchLanguageInfoRequerimentAndOperator = idSearchLanguageInfoRequerimentAndOperator;
    }

    public Integer getIdSearchLanguageInfoRequerimentAndOperator() {
        return idSearchLanguageInfoRequerimentAndOperator;
    }

    public void setIdSearchLanguageInfoRequerimentAndOperator(Integer idSearchLanguageInfoRequerimentAndOperator) {
        this.idSearchLanguageInfoRequerimentAndOperator = idSearchLanguageInfoRequerimentAndOperator;
    }

    public Boolean getRequiredVideo() {
        return requiredVideo;
    }

    public void setRequiredVideo(Boolean requiredVideo) {
        this.requiredVideo = requiredVideo;
    }

    public Language getLanguage() {
        return language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public LanguageLevel getMinimumLanguageLevel() {
        return minimumLanguageLevel;
    }

    public void setMinimumLanguageLevel(LanguageLevel minimumLanguageLevel) {
        this.minimumLanguageLevel = minimumLanguageLevel;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchLanguageInfoRequerimentAndOperator != null ? idSearchLanguageInfoRequerimentAndOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean languageRes=false;
        boolean minimumLanguageLevelRes=false;
        boolean requiredVideoRes=false;
        if (!(object instanceof SearchLanguageInfoRequerimentAndOperator)) {
            return false;
        }
        SearchLanguageInfoRequerimentAndOperator other = (SearchLanguageInfoRequerimentAndOperator) object;
        
        //Language test
        if(this.language==null && other.getLanguage()==null){
            languageRes=true;
        }
        else if(this.language!=null && other.getLanguage()!=null){
            languageRes=this.language.equals(other.getLanguage());
        }
        //MinimumLanguageLevel test
        if(this.getMinimumLanguageLevel()==null && other.getMinimumLanguageLevel()==null){
            minimumLanguageLevelRes=true;
        }
        else if(this.getMinimumLanguageLevel()!=null && other.getMinimumLanguageLevel()!=null){
            minimumLanguageLevelRes=this.getMinimumLanguageLevel().equals(other.getMinimumLanguageLevel());
        }
        //RequiredVideo test
        if(this.getRequiredVideo()==null && other.getRequiredVideo()==null){
            requiredVideoRes=true;
        }
        else if(this.getRequiredVideo()!=null && other.getRequiredVideo()!=null){
            requiredVideoRes=this.getRequiredVideo().equals(other.getRequiredVideo());
        }
        Boolean res=languageRes && minimumLanguageLevelRes && requiredVideoRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchLanguageInfoRequerimentAndOperator[ idSearchLanguageInfoRequerimentAndOperator=" + idSearchLanguageInfoRequerimentAndOperator + " ]";
    }

    public LanguageLevel getWritingLevel() {
        return writingLevel;
    }

    public void setWritingLevel(LanguageLevel writingLevel) {
        this.writingLevel = writingLevel;
    }

    @Override
    public boolean test(Candidate c) {
        boolean pass=false;
        if(c.getCandidateLanguageInfoList()!=null){
            for(CandidateLanguageInfo cli: c.getCandidateLanguageInfoList()){
                boolean validLanguage=getLanguage()==null;
                boolean validWriting=getWritingLevel()==null;
                boolean validSpeaking=getMinimumLanguageLevel()==null;
                boolean validVideo=getRequiredVideo()==null? true : !getRequiredVideo();
                if(getLanguage()!=null && cli.getLanguage()!=null){
                    validLanguage=getLanguage().equals(cli.getLanguage());
                }
                if(getWritingLevel()!=null && cli.getWritingLevel()!=null){
                    validWriting=getWritingLevel().getValue()<=cli.getWritingLevel().getValue();
                }
                if(getMinimumLanguageLevel()!=null && cli.getLanguageLevel()!=null){
                    validSpeaking=getMinimumLanguageLevel().getValue()<=cli.getLanguageLevel().getValue();
                }
                if(getRequiredVideo()!=null && getRequiredVideo()){
                    validVideo=cli.getVideo()!=null;
                }
                pass=validLanguage && validWriting && validSpeaking && validVideo;
                if(pass){
                    break;
                }
            }
        }
        return pass;
    }
    
}
